////
//
/// \file   ./src/symbol_node.cpp
//
//  Copyright 2007 Chandler Carruth
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//
////

#include <inc/parser/symbol_node.hpp>

#include <cassert>
#include <string>

#include <boost/regex.hpp>

#include <inc/parser/parse_tree_node.hpp>

namespace inc {
namespace parser {

using std::string;
using boost::regex;
using boost::match_results;

const regex SymbolNode::symbol_regex( "[~!#$%^&*\\-+=|\\\\:?/<>]+" );

/// \brief  Parse out an INC symbol
/// \author Chandler Carruth
/// \date   2007.06.19
///
/// This parses out an INC symbol from the provided text. The symbol must occur
/// at the beginning of the range provided. When successful, it returns true.
/// It always provides the last parsed character as well, which will always be
/// set equal to the first character of the range when parsing fails.

bool
SymbolNode::parse( const iterator& first,
                   const iterator& last,
                   iterator& last_parsed )
{
  assert( get_text().empty() && "This node was parsed more than once!" );

  match_results<iterator> m;
  if( regex_search( first, last, m, symbol_regex, boost::match_continuous ) )
  {
    set_text( m.str() );
    last_parsed = m.suffix().first;
    return true;
  }

  last_parsed = first;
  return false;
}

} //  end parser namespace
} //  end inc namespace
